# from backend.app.app.db.session import cursor_sys, db_sys
#
# cursor_sys = cursor_sys
# db_sys = db_sys
# # db_sys = pymysql.connect("47.96.235.182", "root", "mysqlUSE80.", "oil_web_project")
# config = {
#     "host": "47.96.235.182",
#     "port": 3306,
#     "username": "root",
#     "password": "mysqlUSE80."
# }
import json
import os
from backend.app.app.crud_pub.authentication import Authentication

auth = Authentication()

path = os.path.join(os.getcwd(), 'db\\config.json')


def load_config(conf_path: str = path):
    """
    加载配置文件
    @param conf_path: 配置文件路径
    @return:
    """
    with open(conf_path, 'r', encoding='utf8') as fp:
        config = json.loads(auth.decryption(fp.read()))
    return config


def update_config(conf: dict, conf_path: str = path):
    """
    更新配置文件
    @param conf_path: 配置文件路径
    @param conf: 配置信息
    @return: 配置信息
    """
    config = load_config(conf_path)
    config['config'].update(conf)
    try:
        with open(conf_path, 'w', encoding='utf8') as fp:
            fp.write(auth.encryption(json.dumps(config)))
        return config
    except:
        return None


# config = load_config("./config.json")['config']
config = load_config()['config']
# update_config({'server_port': 8880}, conf_path="./config.json")
db_engine = 'mysql+pymysql://' + config['username'] + ':' + config['password'] + '@' + config['host'] + ':' + str(
    config['port']) + '/{}?charset=utf8'
# print(load_config("./config.json")['config'])
